package com.example.service.impl;

import com.example.entity.Subject;
import com.example.service.ExcelService;
import com.example.service.JSMindService;
import com.example.service.StudentService;
import com.example.service.SubjectService;
import com.example.utils.File.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.entity.ContentType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author： wzz
 * @date： 2021-11-07 13:16
 */
@Slf4j
@Service
public class ExcelServiceImpl implements ExcelService {

    @Resource
    private StudentService studentService;
    @Resource
    private SubjectService subjectService;
    @Resource
    private JSMindService jsMindService;

    @Override
    public void Import(File file, String book_name, String menu_topic) throws IOException {
        //将file改成multipartFile
        FileInputStream fileInputStream = new FileInputStream(file);
        MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(),
                ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);

        Map<String, String> map=new HashMap<String, String>();    //！hashMap是无序的
        //所有的题目信息
        List<Subject> list = ExcelUtil.readSubjects(multipartFile);
//        System.out.println(list);
        if (list.size() == 0 ){
            map.put("code","1");
            map.put("message","上传失败，请注意格式");
        }else{
            //遍历拿到的excel表格数据
            for (int i=0; i<list.size(); i++){
                //将章节号或者试卷号传入到每个list的item中
                list.get(i).setBook_name(book_name);
                list.get(i).setMenu_topic(menu_topic);
                jsMindService.updateContent(book_name,menu_topic.split(":")[1]);
            }
                int s=subjectService.importSubject(list);

                if (s == 0){
                    map.put("code","100");
                    map.put("message","导入失败");
                }else{
                    log.info("导入成功！！！");
                    map.put("code","200");
                    map.put("message","上传成功");
                }
        }
    }

}
